﻿import QtQuick 2.15
import QtQuick.Controls 2.15
/*
  设备配置界面
*/

Item {
    id:backGround
    width: screenWidth
    height: 1600

    Rectangle{
        anchors.fill: backGround
        color: "#00BFFF"
        border.width: 5
        border.color: "#F0F8FF"
    }

    onVisibleChanged: {
        if(visible === true)
        {
            Cpp_Connect.pageNumber = 19
        }
    }

    property bool gjTimeMode: Cpp_Connect.GJTIME_Mode     // 工进时间 - 模式切换(共用 , 分开)
    property bool lsFunction: Cpp_Connect.LS_Exist     // 拉手功能 - 模式切换(ON , OFF)
    property bool specialy45: Cpp_Connect.Angle45_Mode     // 45度专切 - 模式切换(ON , OFF)
    property bool knobChoose: Cpp_Connect.Knob_Program     // 旋钮选择 - 模式切换(角度 , 机头)
    property bool dcfMode: Cpp_Connect.DCF_Power_Mode        // 电磁阀 - 模式切换(断电 , 不断电)
    property bool bmqMode: Cpp_Connect.Encoder_Mode        // 编码器 - 模式切换(17 , 21)

    property bool printerExist: Cpp_Connect.Printer_Exist   // 打印机是否存在(有 , 没有)
    property bool tljExist: Cpp_Connect.TLJ_Exist       // 托料架是否存在(有 , 没有)
    property bool zdcgFunction: Cpp_Connect.AutoObserve_Exist   // 自动测高功能是否存在(有 ， 没有)
    property bool degrees45Mode: Cpp_Connect.NBWB_Mode  // 45度专切 - 模式切换(内摆 , 外摆)
    property bool scramBtnMode: Cpp_Connect.Scram_Mode   // 急停按钮 - 模式切换(常开 , 常闭)
    property bool servoWarninMode: !Cpp_Connect.Servo_Warning_Mode// 伺服报警 - 模式切换(常开 , 常闭)

    property bool servoType: Cpp_Connect.Servo_Mode      // 伺服类型 - 切换(绝对值伺服 , 增量式伺服)
    property bool chooselrSaw: Cpp_Connect.SawChoose_Mode    // 左右锯选择 - 模式切换(触屏选择 , 旋钮选择)
    property bool xwBtnMode: Cpp_Connect.LimitBtn_Mode    // 限位开关 - 模式切换(常开 , 常闭)
    property bool mechineKnob: Cpp_Connect.JT_Knob_Mode    // 机头旋钮 - 模式切换(常开 , 常闭)
    property bool sawDelayMode: Cpp_Connect.DoubleSaw_Open   // 双头锯延迟 - 模式切换(已启用 , 已禁用)


    Rectangle{      // 工进时间共用 - 模式切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 50
        anchors.left: backGround.left
        anchors.leftMargin: 50
        width: 300
        height: 100
        radius: 10
        color: gjTimeMode ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: gjTimeMode ? qsTr("工进时间分开") : qsTr("工进时间共用")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(gjTimeMode)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("528","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("528","1")
                }
            }
        }
    }

    Rectangle{      // 拉手功能开关 - 模式切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 200
        anchors.left: backGround.left
        anchors.leftMargin: 50
        width: 300
        height: 100
        radius: 10
        color: lsFunction ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: lsFunction ? qsTr("拉手功能 ON") : qsTr("拉手功能 OFF")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(lsFunction)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("529","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("529","1")
                }
            }
        }
    }

    Rectangle{      // 45度专切开关 - 模式切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 350
        anchors.left: backGround.left
        anchors.leftMargin: 50
        width: 300
        height: 100
        radius: 10
        color: specialy45 ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: specialy45 ? qsTr("45度专切 ON") : qsTr("45度专切 OFF")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(specialy45)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("530","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("530","1")
                }
            }
        }
    }

    Rectangle{      // 机头选择开关 - 模式切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 500
        anchors.left: backGround.left
        anchors.leftMargin: 50
        width: 300
        height: 100
        radius: 10
        color: knobChoose ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: knobChoose ? qsTr("旋钮选择角度") : qsTr("旋钮选择机头")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(knobChoose)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("510","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("510","1")
                }
            }
        }
    }

    Rectangle{      // 电磁阀开关 - 模式切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 650
        anchors.left: backGround.left
        anchors.leftMargin: 50
        width: 300
        height: 100
        radius: 10
        color: dcfMode ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: dcfMode ? qsTr("电磁阀断电") : qsTr("电磁阀不断电")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(dcfMode)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("509","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("509","1")
                }
            }
        }
    }

    Rectangle{      // 编码器 - 模式切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 800
        anchors.left: backGround.left
        anchors.leftMargin: 50
        width: 300
        height: 100
        radius: 10
        color: bmqMode ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: bmqMode ? qsTr("编码器 17 位") : qsTr("编码器 21 位")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(bmqMode)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("400","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("400","1")
                }
            }
        }
    }

    Rectangle{      // 打印机是否存在 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 50
        anchors.left: backGround.left
        anchors.leftMargin: 400
        width: 300
        height: 100
        radius: 10
        color: printerExist ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: printerExist ? qsTr("有打印机") : qsTr("没有打印机")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(printerExist)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("398","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("398","1")
                }
            }
        }
    }

    Rectangle{      // 托料架是否存在 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 200
        anchors.left: backGround.left
        anchors.leftMargin: 400
        width: 300
        height: 100
        radius: 10
        color: tljExist ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: tljExist ? qsTr("有托料架") : qsTr("没有托料架")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(tljExist)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("397","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("397","1")
                }
            }
        }
    }

    Rectangle{      // 自动测高功能是否存在 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 350
        anchors.left: backGround.left
        anchors.leftMargin: 400
        width: 300
        height: 100
        radius: 10
        color: zdcgFunction ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: zdcgFunction ? qsTr("有自动测高功能") : qsTr("没有自动测高功能")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(zdcgFunction)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("396","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("396","1")
                }
            }
        }
    }

    Rectangle{      // 45度专切 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 500
        anchors.left: backGround.left
        anchors.leftMargin: 400
        width: 300
        height: 100
        radius: 10
        color: degrees45Mode ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: degrees45Mode ? qsTr("45度内摆") : qsTr("45度外摆")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()

                if(!degrees45Mode)
                {
                    console.log("当前需要写入" + "true")
                    Cpp_Connect.write_modbus_rtu_Coils("394","1")
                }
                else
                {
                    console.log("当前需要写入" + "false")
                    Cpp_Connect.write_modbus_rtu_Coils("394","0")
                }                
            }
        }
    }

    Rectangle{      // 急停按钮模式 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 650
        anchors.left: backGround.left
        anchors.leftMargin: 400
        width: 300
        height: 100
        radius: 10
        color: scramBtnMode ?  "#006cee" : "#ff3606"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: scramBtnMode ? qsTr("急停按钮常开") : qsTr("急停按钮常闭")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(scramBtnMode)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("500","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("500","1")
                }
            }
        }
    }

    Rectangle{      // 伺服报警模式 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 800
        anchors.left: backGround.left
        anchors.leftMargin: 400
        width: 300
        height: 100
        radius: 10
        color: servoWarninMode ?  "#006cee" : "#ff3606"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: servoWarninMode ? qsTr("伺服报警常开") : qsTr("伺服报警常闭")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(servoWarninMode)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("501","1")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("501","0")
                }
            }
        }
    }

    Rectangle{      // 伺服类型 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 50
        anchors.left: backGround.left
        anchors.leftMargin: 750
        width: 300
        height: 100
        radius: 10
        color: servoType ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: servoType ? qsTr("绝对值伺服") : qsTr("增量式伺服")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(servoType)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("392","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("392","1")
                }
            }
        }
    }

    Rectangle{      // 左右锯选择 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 200
        anchors.left: backGround.left
        anchors.leftMargin: 750
        width: 300
        height: 100
        radius: 10
        color: chooselrSaw ?  "#ff3606" : "#006cee"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: chooselrSaw ? qsTr("触屏选择左右锯") : qsTr("旋钮选择左右锯")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(chooselrSaw)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("393","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("393","1")
                }
            }
        }
    }

    Rectangle{      // 限位开关 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 350
        anchors.left: backGround.left
        anchors.leftMargin: 750
        width: 300
        height: 100
        radius: 10
        color: xwBtnMode ?  "#006cee" : "#ff3606"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: xwBtnMode ? qsTr("限位开关常开") : qsTr("限位开关常闭")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(xwBtnMode)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("502","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("502","1")
                }
            }
        }
    }

    Rectangle{      // 机头旋钮 - 切换按钮
        anchors.top: backGround.top
        anchors.topMargin: 500
        anchors.left: backGround.left
        anchors.leftMargin: 750
        width: 300
        height: 100
        radius: 10
        color: mechineKnob ?  "#006cee" : "#ff3606"
        border.width: 1
        border.color: "#f0f8ff"

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: mechineKnob ? qsTr("机头旋钮常开") : qsTr("机头旋钮常闭")
        }

        MouseArea{
            anchors.fill: parent

            onClicked: {
                parent.forceActiveFocus()
                if(mechineKnob)
                {
                    Cpp_Connect.write_modbus_rtu_Coils("503","0")
                }
                else
                {
                    Cpp_Connect.write_modbus_rtu_Coils("503","1")
                }
            }
        }
    }

    Rectangle{      // 机头旋钮 - 切换按钮
      anchors.top: backGround.top
      anchors.topMargin: 800
      anchors.left: backGround.left
      anchors.leftMargin: 750
      width: 300
      height: 100
      radius: 10
      color:  flag ? "#ff3606" : "#006cee"
      border.width: 1
      border.color: "#f0f8ff"

      property bool flag: false

      Text{
        anchors.centerIn: parent
        font.pixelSize: 30
        font.bold: true
        text:  qsTr("手动回零点")
      }

      MouseArea{
        anchors.fill: parent

        onPressed: {
          parent.flag = true

          Cpp_Connect.write_modbus_rtu_Coils("23","1")
          Cpp_Connect.write_modbus_rtu_Coils("5","1")
        }

        onReleased: {
          parent.flag = false
          Cpp_Connect.write_modbus_rtu_Coils("23","0")
          Cpp_Connect.write_modbus_rtu_Coils("5","0")
        }

      }
    }

    Rectangle{
        anchors.top: backGround.top
        anchors.topMargin: 950
        anchors.horizontalCenter: backGround.horizontalCenter
        color: "#00BFFF"
        border.width: 5
        border.color: "#F0F8FF"
        width: backGround.width
        height: 300

        Text{
            anchors.left: parent.left
            anchors.leftMargin: 30
            anchors.top: parent.top
            anchors.topMargin: 60
            font.pixelSize: 30
            font.bold: true
            text: qsTr("双锯片延时")
        }

        Rectangle{
            anchors.top: parent.top
            anchors.topMargin: 30
            anchors.left: parent.left
            anchors.leftMargin: 210
            width: 200
            height: 100
            radius: 10
            enabled: sawDelayMode
            color: sawDelayMode ? "#ffffff" : "lightgray"

            Text{
                id:sawDelayTime_Text
                anchors.centerIn: parent
                font.pixelSize: 30
                font.bold: true
                text: Cpp_Connect.DoubleSaw_Delay
                visible: true
            }

            TextInput{          // 双锯片延时输入框
                id:sawDelayTime_Input
                anchors.centerIn: parent
                font.pixelSize: 30
                font.bold: true
                font.italic: true
                visible: false


                onEditingFinished: {
                    Cpp_Connect.write_modbus_rtu_Hold("1562",sawDelayTime_Input.text)
                    sawDelayTime_Input.visible = false
                    sawDelayTime_Text.visible = true
                }

            }

            MouseArea{
                anchors.fill: parent

                onClicked: {
                    sawDelayTime_Input.forceActiveFocus()
                    sawDelayTime_Input.text = sawDelayTime_Text.text
                    sawDelayTime_Input.visible = true
                    sawDelayTime_Text.visible = false
                    sawDelayTime_Input.selectAll()
                }
            }
        }

        Text {
            anchors.left: parent.left
            anchors.leftMargin: 450
            anchors.top: parent.top
            anchors.topMargin: 60
            font.bold: true
            font.pixelSize: 30
            text: qsTr("左右锯片依次启动时的延时时间")
        }


        Rectangle{
            anchors.right: parent.right
            anchors.rightMargin: 30
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 30
            width: 200
            height: 100
            color: sawDelayMode ? "#ff3606" : "#006cee"
            radius: 10
            border.width: 1
            border.color: "#f0f8ff"

            Text{
                anchors.centerIn: parent
                font.pixelSize: 30
                font.bold: true
                text: sawDelayMode ? qsTr("已启用") : qsTr("已禁用")
            }

            MouseArea{
                anchors.fill: parent

                onClicked: {
                    parent.forceActiveFocus()
                    sawDelayMode = !sawDelayMode
                }
            }

        }

    }

    Rectangle{
        anchors.left: backGround.left
        anchors.leftMargin: 30
        anchors.bottom: backGround.bottom
        anchors.bottomMargin: 30
        radius: 10
        color: "#006cee"
        border.width: 1
        border.color: "#f0f8ff"
        width: 200
        height: 100

        Text{
            anchors.centerIn: parent
            font.pixelSize: 30
            font.bold: true
            text: qsTr("返  回")
        }

        MouseArea{
            anchors.fill: parent

            onPressed: {
                parent.color = "#ff3606"
            }

            onReleased: {
                parent.color = "#006cee"
                deviceLayoutWindow.visible = false
                advanceSettingWindow.visible = true
            }
        }
    }
}
